home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / c_eval.zip / EE.C next >
Text File  |  1993-04-23  |  27KB  |  769 lines

  1. /*************************************************************************
  2. **                                                                       **
  3. ** EE.C         Expression Evaluator                                     **
  4. **                                                                       **
  5. ** AUTHOR:      Mark Morley                                              **
  6. ** COPYRIGHT:   (c) 1992 by Mark Morley                                  **
  7. ** DATE:        December 1991                                            **
  8. ** HISTORY:     Jan 1992 - Made it squash all command line arguments     **
  9. **                         into one big long string.                     **
  10. **                       - It now can set/get VMS symbols as if they     **
  11. **                         were variables.                               **
  12. **                       - Changed max variable name length from 5 to 15 **
  13. **              Jun 1992 - Updated comments and docs                     **
  14. **                                                                       **
  15. ** You are free to incorporate this code into your own works, even if it **
  16. ** is a commercial application.  However, you may not charge anyone else **
  17. ** for the use of this code!  If you intend to distribute your code,     **
  18. ** I'd appreciate it if you left this message intact.  I'd like to       **
  19. ** receive credit wherever it is appropriate.  Thanks!                   **
  20. **                                                                       **
  21. ** I don't promise that this code does what you think it does...         **
  22. **                                                                       **
  23. ** Please mail any bug reports/fixes/enhancments to me at:               **
  24. **      morley@camosun.bc.ca                                             **
  25. ** or                                                                    **
  26. **      Mark Morley                                                      **
  27. **      3889 Mildred Street                                              **
  28. **      Victoria, BC  Canada                                             **
  29. **      V8Z 7G1                                                          **
  30. **      (604) 479-7861                                                   **
  31. **                                                                       **
  32.  *************************************************************************/
  33.  
  34. /* #define VAX */             /* Uncomment this line if you're using VMS */
  35.  
  36. #include <stdlib.h>
  37. #include <math.h>
  38. #include <setjmp.h>
  39.  
  40. #ifdef VAX
  41. #include <ssdef.h>
  42. #include <descrip.h>
  43. #endif
  44.  
  45. #include "ee.h"
  46.  
  47. #define ERR(n) {ERROR=n; ERPOS=expression-ERANC-1; strcpy(ERTOK,token); longjmp(jb,1);}
  48.  
  49. /* These defines only happen if the values are not already defined!  You may
  50.    want to add more precision here, if your machine supports it. */
  51. #ifndef M_PI
  52. #define M_PI    3.14159265358979323846
  53. #endif
  54. #ifndef M_E
  55. #define M_E     2.71828182845904523536
  56. #endif
  57.  
  58.  
  59. /*************************************************************************
  60. **                                                                       **
  61. ** PROTOTYPES FOR CUSTOM MATH FUNCTIONS                                  **
  62. **                                                                       **
  63.  *************************************************************************/
  64.  
  65. double deg( double x );
  66. double rad( double x );
  67.  
  68.  
  69. /*************************************************************************
  70. **                                                                       **
  71. ** VARIABLE DECLARATIONS                                                 **
  72. **                                                                       **
  73.  *************************************************************************/
  74.  
  75. int   ERROR;                 /* The error number */
  76. char  ERTOK[TOKLEN + 1];     /* The token that generated the error */
  77. int   ERPOS;                 /* The offset from the start of the expression */
  78. char* ERANC;                 /* Used to calculate ERPOS */
  79.  
  80. /*
  81.    Add any "constants" here...  These are "read-only" values that are
  82.    provided as a convienence to the user.  Their values can not be
  83.    permanently changed.  The first field is the variable name, the second
  84.    is its value.
  85. */
  86. VARIABLE Consts[] =
  87. {
  88.    /* name, value */
  89.    { "pi",      M_PI },
  90.    { "e",       M_E },
  91.  
  92.    { 0 }
  93. };
  94.  
  95. /*
  96.    Add any math functions that you wish to recognise here...  The first
  97.    field is the name of the function as it would appear in an expression.
  98.    The second field tells how many arguments to expect.  The third is
  99.    a pointer to the actual function to use.
  100. */
  101. FUNCTION Funcs[] =
  102. {
  103.    /* name, funtion to call */
  104.    { "sin",     1,    sin },
  105.    { "cos",     1,    cos },
  106.    { "tan",     1,    tan },
  107.    { "asin",    1,    asin },
  108.    { "acos",    1,    acos },
  109.    { "atan",    1,    atan },
  110.    { "sinh",    1,    sinh },
  111.    { "cosh",    1,    cosh },
  112.    { "tanh",    1,    tanh },
  113.    { "exp",     1,    exp },
  114.    { "log",     1,    log },
  115.    { "log10",   1,    log10 },
  116.    { "sqrt",    1,    sqrt },
  117.    { "floor",   1,    floor },
  118.    { "ceil",    1,    ceil },
  119.    { "abs",     1,    fabs },
  120.    { "hypot",   2,    hypot },
  121.  
  122.    { "deg",     1,    deg },
  123.    { "rad",     1,    rad },
  124.  
  125.    { 0 }
  126. };
  127.  
  128. VARIABLE        Vars[MAXVARS];       /* Array for user-defined variables */
  129. unsigned char*  expression;          /* Pointer to the user's expression */
  130. unsigned char   token[TOKLEN + 1];   /* Holds the current token */
  131. int             type;                /* Type of the current token */
  132. jmp_buf         jb;                  /* jmp_buf for errors */
  133.  
  134.  
  135. /*************************************************************************
  136. **                                                                       **
  137. ** Some custom math functions...   Note that they must be prototyped     **
  138. ** above (if your compiler requires it)                                  **
  139. **                                                                       **
  140. ** deg( x )             Converts x radians to degrees.                   **
  141. ** rad( x )             Converts x degrees to radians.                   **
  142. **                                                                       **
  143.  *************************************************************************/
  144.  
  145. double
  146. deg( double x )
  147. {
  148.    return( x * 180.0 / M_PI );
  149. }
  150.  
  151. double
  152. rad( double x )
  153. {
  154.    return( x * M_PI / 180.0 );
  155. }
  156.  
  157.  
  158. /*************************************************************************
  159. **                                                                       **
  160. ** GetSymbol( char* s )                                                  **
  161. **                                                                       **
  162. ** This routine obtains a value from the program's environment.          **
  163. ** This works for DOS and VMS (and other OS's???)
  164. **                                                                       **
  165.  ************************************************************************/
  166.  
  167. GetSymbol( char* s, TYPE* v )
  168. {
  169.    char* e;
  170.  
  171.    e = getenv( s );
  172.    if( !e )
  173.       return( 0 );
  174.    *v = atof( e );
  175.    return( 1 );
  176. }
  177.  
  178.  
  179. /*************************************************************************
  180. **                                                                       **
  181. ** SetSymbol( char* s, char* v )                                         **
  182. **                                                                       **
  183. ** This VMS specific routine sets (or updates) a VMS symbol to a given   **
  184. ** value                                                                 **
  185. **                                                                       **
  186.  *************************************************************************/
  187.  
  188. #ifdef VAX
  189. SetSymbol( char* s, char* v )
  190. {
  191.    struct dsc$descriptor_s sym;
  192.    struct dsc$descriptor_s val;
  193.    long                    typ = 1;
  194.  
  195.    sym.dsc$w_length = strlen( s );
  196.    sym.dsc$a_pointer = s;
  197.    sym.